Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support UDP in wsl-proxy #7618

Merged
merged 4 commits into from
Oct 11, 2024
Merged

Support UDP in wsl-proxy #7618

merged 4 commits into from
Oct 11, 2024

Conversation

Nino-K
Copy link
Member

@Nino-K Nino-K commented Oct 10, 2024

The wsl-proxy previously only supported TCP connections. With this change, it now enables the listening and handling of UDP packets in the proxy.

Fixes: #6821
Also, addresses: #7473 and #4873

@Nino-K Nino-K requested a review from mook-as October 10, 2024 17:36
@Nino-K Nino-K force-pushed the add-udp-support-wsl-proxy branch 3 times, most recently from 5040319 to 6f2b119 Compare October 10, 2024 18:15
Copy link
Contributor

@mook-as mook-as left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be new tests to check that this works? It looks like we have an existing test for TCP?

)

const (
defaultLogPath = "/var/log/wsl-proxy.log"
defaultSocket = "/run/wsl-proxy.sock"
bridgeIPAddr = "192.168.143.1"
// Set UDP buffer size to 8 MB
defaultUDPBufferSize = 8388608 // 8 MB in bytes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would something like 8 * 1024 * 1024 be clearer?

)

func main() {
flag.BoolVar(&debug, "debug", false, "enable additional debugging.")
flag.StringVar(&logFile, "logfile", defaultLogPath, "path to the logfile for wsl-proxy process")
flag.StringVar(&socketFile, "socketFile", defaultSocket, "path to the .sock file for UNIX socket")
flag.StringVar(&upstreamAddr, "upstreamAddress", bridgeIPAddr, "IP address of the upstream server to forward to")
flag.IntVar(&udpBuffer, "udpBuffer", defaultUDPBufferSize, "max buffer size for the UDP socket io")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
flag.IntVar(&udpBuffer, "udpBuffer", defaultUDPBufferSize, "max buffer size for the UDP socket io")
flag.IntVar(&udpBuffer, "udpBuffer", defaultUDPBufferSize, "max buffer size in bytes for UDP socket I/O")

I started adding in bytes and then couldn't help myself LOL…

if gvisorTypes.TransportProtocol(portProto.Proto()) == gvisorTypes.TCP {
p.handleTCP(portBindings, pm.Remove)
}
if gvisorTypes.TransportProtocol(portProto.Proto()) == gvisorTypes.UDP {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else if? Or even a switch(….Proto())?

if remove {
p.udpConnMutex.Lock()
if udpConn, exist := p.activeUDPConns[port]; exist {
logrus.Debugf("closing UDPConn for port: %d", port)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debugf with a mutex held seems kind of heavy?

Copy link
Member Author

@Nino-K Nino-K Oct 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, however, since it's only going to output when debugging is on, do you still think that would be an issue? But, regardless I'll move it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's also a potential Errorf later. But yeah, it's probably fine.

continue
}

p.udpConnMutex.Lock()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need the mutex held for all of this, or just the one line after?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, unnecessary defer.

src/go/networking/pkg/portproxy/server.go Show resolved Hide resolved
for {
b := make([]byte, p.config.UDPBufferSize)
n, addr, err := sourceConn.ReadFromUDP(b)
if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per PacketConn documentation:

Callers should always process the n > 0 bytes returned before considering the error err.

@Nino-K Nino-K force-pushed the add-udp-support-wsl-proxy branch 2 times, most recently from a499776 to 932e27b Compare October 11, 2024 17:11
The wsl-proxy previously only supported TCP connections.
With this change, it now enables the listening and handling
of UDP packets in the proxy.

Signed-off-by: Nino Kodabande <[email protected]>
Signed-off-by: Nino Kodabande <[email protected]>
mook-as
mook-as previously approved these changes Oct 11, 2024
src/go/networking/pkg/portproxy/server.go Show resolved Hide resolved
for {
udpMappings = portProxy.UDPPortMappings()
if len(udpMappings) > 0 {
close(ready)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why this needs to be a separate goroutine, instead of just a normal loop, but 🤷

for len(portProxy.UDPPortMappings()) == 0 {
  time.Sleep(100 * time.Millisecond)
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct, my original approach was polling for Port (before implementing UDPPortMappings) was doing things in a goroutine, but since we have the function, there is no need for a goroutine, removed.

@Nino-K Nino-K enabled auto-merge October 11, 2024 18:49
@Nino-K Nino-K merged commit 6cf793b into main Oct 11, 2024
27 checks passed
@Nino-K Nino-K deleted the add-udp-support-wsl-proxy branch October 11, 2024 19:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

UDP port forwarding not working on Windows with network tunnel enabled
2 participants